package com.tianji.learning.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.tianji.learning.utils.TableInfoContext;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 19311
 * @CreateTime: 2025-03-28
 * @Description: 通过Mp配置动态表名拦截器
 * @Version: 1.0
 */
@Configuration
public class MybatisConfiguration {
    /**
     * MybatisPlusInterceptor 配置动态表名拦截器
     * @return
     */
    @Bean
    public DynamicTableNameInnerInterceptor dynamicTableName() {
        Map<String, TableNameHandler> mp = new HashMap<>();
        mp.put("points_board", (sql, tableName) -> TableInfoContext.getTableName());
        return new DynamicTableNameInnerInterceptor(mp);
    }
}
